<template>
  <div class="edit-gender">
    <van-picker
      title="请选择性别"
      show-toolbar
      :columns="genders"
      :default-index="value"
      @confirm="onConfirm"
      @cancel="$emit('close')"
      @change="onChange"
    />
  </div>
</template>

<script>
import { editUserList } from '@/api/users'
export default {
  name: 'EditGender',
  props: {
    value: {
      type: Number,
      require: true
    }

  },
  data () {
    return {
      genders: ['男', '女'],
      localGender: this.value
    }
  },
  methods: {
    onChange (picker, value, index) {
      // console.log(picker, value, index)
      this.localGender = index
      console.log(index)
    },
    async onConfirm () {
      this.$toast.loading({
        message: '保存中....',
        forbidClick: true,
        duration: 0
      })
      try {
        const localGender = this.localGender
        await editUserList({
          gender: localGender
        })
        this.$toast.success('修改性别成功')
        this.$emit('input', localGender)
        this.$emit('close')
      } catch (err) {
        this.$toast('修改用户性别失败')
      }
    }
  }
}
</script>

<style lang="less" scoped>
</style>
